
import Common;
// LT is now a shortcut for org.sugar.utilities.ListTools

import flash.display.MovieClip;
import flash.display.Stage;
class ListDemo {
	public static function main(){
		
		var arr = new Array<String>();
		arr = ['key 1', 'key 2', 'key 3'];
		var hsh = new Hash<Float>();
		for (i in arr){
		  hsh.set(i,Math.random()); // set keys to random floats
		}

		
		var sorter:String->String->Int = function(x,y){ 
		    if (hsh.get(x) > hsh.get(y)){return 1;}
		    else if (hsh.get(x) < hsh.get(y)){return -1;}
		    else {return 0;}
		}

		arr.sort(sorter);

		for (i in arr){
		  trace(i + ': ' + hsh.get(i));
		}
		
		
		var arr = [1,2,3,4];
		for (i in 0...20){
		
			trace(AT.permutationIndex(5,i));
		}
		
		return;
		
		
		var x = LT.ii(1...5);  // create an Iterable from an IntIter
		Lambda.map(x,function(y) trace(y));
		
		// keep in mind, x is now an "empty" Iterable, so you can't use it again
 		Lambda.map(x,function(y) trace(y)); //won't trace anything
		
		// You can use itrMap() with IntIters...
		LT.itrMap(1...5, function(x) trace(x));
		
		var lint = new List<Int>();  // list of ints
		lint.add(1); lint.add(2); lint.add(3);
		
		var lstring = new List<String>(); // list of strings
		lstring.add('one'); lstring.add('two'); lstring.add('three');
		
		trace(LT.concat(lint,lint));
//		trace(LT.concat(lint,lstring)); // this will throw an error, types must match
		
		trace(LT.reverse(lint)); // a reverse copy of the list
		
		
	   // cumulative sum using scan
		trace(LT.scan(LT.ii(1...5), function(value,accum) return accum+=value, 0));
		
		// zip two or more lists together into a list of their paired elements using zip()

		trace(LT.zip([lint, lstring, lint]));

		var z = LT.zip([lint, lstring, lint]);
	/*	trace(LT.unzip(z));
			trace(LT.unzip([lint, lstring, lint]));
			*/
		
		var s = new Array<Dynamic>();
		s.push([1,2]); s.push([2,3]);
		trace(LT.zip(s));
		trace(LT.unzip(s));
		
		var intarray:Array<Int> = [1,2, 3];
		var floatarray:Array<Float> = [1.2,1.3, 1.4];		
		
		var l = LT.unzip([intarray, floatarray]);
	
		trace(l);
		trace(LT.unzip([[1,2,3],[7, 3.0,2,1]]));
		var l = new List<Float>(); l.add(1); l.add(4); l.add(9); l.add(0);
		trace(LT.max(l));
		
		
		
		

/*		trace(LT.unzip([[1,2],[3,4]]));*/ // doesn't work yet!
		
	
		// print out the parents for a given MovieClip using unfold
		var x = new MovieClip(); x.name = 'x'; x.x = 1.4;
		var y = new MovieClip(); y.name = 'y'; y.x = 1.9;
		var z = new MovieClip(); z.name = 'z';
		trace(LT.fieldMap([x,y,z], 'name'));

		
		trace(IT.fieldItr([x,y,z],'name'));
		
		for (i in IT.fieldItr([x,y,z],'name')){trace(i);}
		trace('hi');
		return;
	
		flash.Lib.current.addChild(x); x.addChild(y); y.addChild(z);
		
		var path = LT.unfold(z, //seed
									function(x) return x.name, //transformer
									function(x) return cast(x.parent,MovieClip), //incrementor
									function(x) return Type.getClass(x.parent) != Stage && x.parent != null // predicate
									); 
		
		trace(path);			
		


		
	
		var l = LT.itrMap(1...10, function(x){return x;});
		// convert an iterable to a FastList
		trace(LT.fastList(l));
		
		var sorter:Int->Int->Int = function(x,y){   
				if (x > y){return 1;}
				else if (x < y){return -1;}
				else {return 0;}
			}
			
		var arr = [9,2,3,5,7,1];
		trace(arr);
		trace(LT.sort(arr, sorter));
		
	}
}